package com.hq.fxh.No5_User.MyModle.Activity.item;

import android.content.Context;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hq.fxh.CustomerView.switchbutton.SwitchButton;
import com.hq.fxh.No5_User.MyModle.AdBannerInfo;
import com.hq.fxh.No5_User.MyModle.ClearableEidtText;
import com.hq.fxh.R;
import com.hq.fxh.Utils.GlideUtils;


/**
 * @author 大圣
 * @ClassName BannerAdItem
 * @Description : 通栏广告item
 * @Created 2015/11/13 0013
 * @Email qwqe601660957@163.com
 */
public class BannerAdItem extends LinearLayout {
//    private FrameLayout flHead;
    /**
     * 删除按钮
     */
    private ImageButton ibClose;
    /**
     * 图片添加
     */
    private FrameLayout flAdPicAdd;
    /**
     * 广告类型名称
     */
    private TextView tvAdTypeName;
    /**
     * 通栏广告图片
     */
    private ImageView ivAd;
    private LinearLayout llEdit;
    /**
     * 链接
     */
    private ClearableEidtText etName;
    //    private TextView tvDes;
    private SwitchButton sbSwitch;
    private TextView tvSwitchInfo;
    private AppCompatCheckBox cbUseSpace;
    boolean flag = false;
    String url = null;//图片url
    private AdBannerInfo adBannerInfo;

    public BannerAdItem(Context context) {
        super(context);
        inflate(context, R.layout.item_banner_ad, this);
        findView();
        tvAdTypeName.setText("通栏广告");
    }


    public void findView() {
        flAdPicAdd = (FrameLayout) findViewById(R.id.flAdPicAdd);
        ibClose = (ImageButton) findViewById(R.id.ibClose);
        tvAdTypeName = (TextView) findViewById(R.id.tvAdTypeName);
        ivAd = (ImageView) findViewById(R.id.ivAd);
        etName = (ClearableEidtText) findViewById(R.id.etName);
        llEdit = (LinearLayout) findViewById(R.id.llEdit);
        sbSwitch = (SwitchButton) findViewById(R.id.sbSwitch);
        tvSwitchInfo = (TextView) findViewById(R.id.tvSwitchInfo);
        cbUseSpace = (AppCompatCheckBox) findViewById(R.id.cbUseSpace);

    }

    public void setInfo(final AdBannerInfo info) {//网络请求回来后（从服务器获取到的已保存模板）
        adBannerInfo = info;
        if (info.getAdvert_postion() == 2) {
            tvAdTypeName.setText("通栏广告");
        } else {
        }

        AdBannerInfo.AdvertDetailBean Advert_detail = info.getAdvert_detail();
        if (Advert_detail != null) {
            etName.setText(Advert_detail.getHref());
            //      ImageLoader.getInstance().load(getContext(), ivAd, Advert_detail.getPic());
            String pic = Advert_detail.getPic();
            if (pic == null || pic.equals("")) {

            } else {
                GlideUtils.loadImageView(getContext(), pic, ivAd);

            }
        } else {
            Advert_detail = new AdBannerInfo.AdvertDetailBean();
            info.setAdvert_detail(Advert_detail);
        }
        etName.setFilters(new InputFilter[]{new EmojiFilter()});
        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = s.toString();
                AdBannerInfo.AdvertDetailBean advertDetailBean = info.getAdvert_detail();
                if (advertDetailBean == null) {
                    advertDetailBean = new AdBannerInfo.AdvertDetailBean();
                }
                if (str.contains("【")
                        || str.contains("】")
                        || str.contains("，")
                        || str.contains("：")
                        || str.contains("；")) {
                    Toast.makeText(getContext(), "链接不能包含特殊字符", Toast.LENGTH_SHORT).show();
                    // ToastUtils.show(getContext(), "链接不能包含特殊字符");
                }
                advertDetailBean.setHref(str.toString());
                info.setAdvert_detail(advertDetailBean);
            }
        });
        sbSwitch.setVisibility(View.VISIBLE);
        sbSwitch.setChecked(BooleanUtils.intToBool(info.getIs_carousel()));
//        setText(sbSwitch.isChecked());
        sbSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                info.setIs_carousel(BooleanUtils.BoolToInt(isChecked));
            }
        });
        cbUseSpace.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                flag = !flag;
                cbUseSpace.setChecked(flag);
            /*    final User user = null;
                if (user != null) {
                    if (!user.getIsHaveHomePage()) {
                        Toast.makeText(getContext(),"您暂未开通形象空间，请前往 我的-形象空间设置。",Toast.LENGTH_SHORT).show();
                     }
                }*/
                if (isChecked) {
               /*     int uid = SharedPreferenceUtils.getMemberId(getContext());
//                    String url = "http://com.ledianduo.com/xxkj/index.html?MerchantId=" + uid;
//                    String url= CommonUtils.getLddConfig().getCdnSite()+"/xxkj/index.html?MerchantId="+ uid;
                    String url=SharedPreferenceUtils.getSpaceImageUrl(getContext());
                    info.getAdvert_detail().setHref(url);
                    etName.setText(url);*/
                } else {
                    info.getAdvert_detail().setHref("");
                    etName.setText("");
                }
            }
        });


    }

//    public void setSbSwitchText(boolean isCheck, int checkShow) {
//        if (isCheck) {
//            tvSwitchInfo.setTextColor(this.getResources().getColor(R.color.new_black));
//            tvSwitchInfo.setTextSize(14);
//            tvSwitchInfo.setText(R.string.ad_on_tip);
//            if (checkShow > 0) {
//                tvSwitchInfo.setTextColor(this.getResources().getColor(R.color.new_black));
//                tvSwitchInfo.setTextSize(14);
//                ToastUtils.show(getContext(), R.string.to_show);
//            }
//
//        } else {
//            tvSwitchInfo.setText(R.string.ad_off_tip);
//            if (checkShow > 0) {
//                ToastUtils.show(getContext(), R.string.to_hide);
//            }
//
//        }
//    }

    public void setText(boolean isCheck) {
        if (isCheck) {
            tvSwitchInfo.setText(R.string.ad_on_tip);

        } else {
            tvSwitchInfo.setText(R.string.ad_off_tip);

        }
    }

//    private boolean reset;

    public void setOnClickListener(final OnClickListener listener) {
        ibClose.setOnClickListener(listener);
        flAdPicAdd.setOnClickListener(listener);
    }

    public void setImage(String url) {
        //      ImageLoader.getInstance().load(getContext(), ivAd, url);
        GlideUtils.loadImageView(getContext(), url, ivAd);

        if (adBannerInfo != null && adBannerInfo.getAdvert_detail() != null) {
            adBannerInfo.getAdvert_detail().setPic(url);
        } else {
            AdBannerInfo.AdvertDetailBean advertDetailBean = new AdBannerInfo.AdvertDetailBean();
            advertDetailBean.setPic(url);
            advertDetailBean.setHref("");
            adBannerInfo.setAdvert_detail(advertDetailBean);
        }
    }
}
